<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Handlers\SchoolJobHandler;

class PullSchoolJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'school:job';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get school job';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = SchoolJobHandler::jobs();
        $today = $ago = [];
        if (empty($data)) {
            return false;
        }
        foreach ($data as $v) {
            if ($v['date'] == date('Ymd')) {
                $today[] = $v;
            } else {
                $ago[] = $v;
            }
        }
        unset($data);
        
        $this->sendEmail(compact('ago', 'today'));
	info('发送短信成功');
        $this->info('发送邮件成功，请查收！');
    }

    public function sendEmail($data)
    {
        Mail::send('email.schoolJob', $data, function($message){
            $to = env('MAIL_TO_ADDRESS');
            $message ->to($to)->subject('新的一天，新的开始New！');
        });
    }
}
